/*
 * Copyright (c) 2024.
 * @Copyright (c) 2024. Enigma_HF All Rights Reserved
 * @File:TestController.java
 * @Author: Enigma_HF
 * @Function:
 * @Date: 2024/5/27 下午3:57
 * @version:1.0
 * @Changes:
 * @Description:
 */

package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.bean.*;
import com.example.demo.dao.TestDao;
import com.example.demo.mapper.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ToolController {

    @Autowired
    private TestDao testDao;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ToolTypeMapper toolTypeMapper;
    @Autowired
    private ToolMapper toolMapper;
    @Autowired
    private BorrowMapper borrowMapper;
    @Autowired
    private SupplyMapper supplyMapper;

    @RequestMapping("/hello")
    String helloWorld() {
        return "Hello World!";
    }

    @RequestMapping("/static/hello1")
    String helloWorld1() {
        return "hello 中文  world!!";
    }

    @RequestMapping("/testSQLType")
    String testMySQL() {
        return testDao.testSQLType();
    }


    @RequestMapping("/login")
    Object login(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        //User exist = userMapper.selectOne(new QueryWrapper<User>(user).eq("name",user.getName()).eq("passwd",user.getPasswd()));
        User exist = userMapper.selectOne(new QueryWrapper<User>(user));

        ObjectMapper objectMapper = new ObjectMapper();

        if(exist != null){
            map.put("user", exist);
            map.put("msg","success");
        }else{
            map.put("msg","error") ;
        }

        return map;
    }

    @RequestMapping("/register")
    String register(@RequestBody  User user) {

        User newUser = new User();
        newUser.setName(user.getName());
        User exist = userMapper.selectOne(new QueryWrapper<User>(newUser));
        if(exist != null){
            return "exist";
        }
        int num = userMapper.insert(user);
        if(num > 0 ){
            return "success";
        }
        return "error";
    }


    @RequestMapping("/toolType/add")
    String addToolType(@RequestBody ToolType toolType) {
        int num = toolTypeMapper.insert(toolType);
        return num > 0 ? "success" : "error";
    }



    @RequestMapping("/toolType/list")
    Object listToolType(@RequestBody ToolType toolType) {
        Map<String, Object> map = new HashMap<>();

        List<ToolType> toolTypeIPage = toolTypeMapper.selectList(new QueryWrapper<>(toolType));
        map.put("msg","success");
        map.put("records",toolTypeIPage);

        return map;
    }


    @RequestMapping("/toolType/update")
    String updateToolType(@RequestBody ToolType toolType) {
        int num = toolTypeMapper.updateById(toolType);
        return num > 0 ? "success" : "error";
    }


    @RequestMapping("/toolType/del")
    String delToolType(@RequestBody Tool toolType) {
        int num = toolTypeMapper.deleteById(toolType);
        return num > 0 ? "success" : "error";
    }




    @RequestMapping("/tool/add")
    String addTool(@RequestBody Tool toolType) {
        int num = toolMapper.insert(toolType);
        return num > 0 ? "success" : "error";
    }



    @RequestMapping("/tool/list")
    Object listTool(@RequestBody Tool toolType) {
        Map<String, Object> map = new HashMap<>();

        List<Tool> toolTypeIPage = toolMapper.selectList(new QueryWrapper<>(toolType));
        map.put("msg","success");
        map.put("records",toolTypeIPage);

        return map;
    }


    @RequestMapping("/tool/update")
    String updateTool(@RequestBody Tool toolType) {
        int num = toolMapper.updateById(toolType);
        return num > 0 ? "success" : "error";
    }


    @RequestMapping("/tool/del")
    String delTool(@RequestBody Tool toolType) {
        int num = toolMapper.deleteById(toolType);
        return num > 0 ? "success" : "error";
    }



    @RequestMapping("/tool/borrow")
    String borrow(@RequestBody Borrow toolType) {
        int num = borrowMapper.insert(toolType);
        if(num > 0 ){
            num = toolMapper.update(null,new UpdateWrapper<Tool>().setSql("num= CAST(CAST(num AS UNSIGNED) - "+ toolType.getNum() +" AS CHAR)" ).eq("name",toolType.getTool()));
        }

        return num > 0 ? "success" : "error";
    }


    @RequestMapping("/tool/log")
    Object borrowList(@RequestBody Borrow toolType) {

        Map<String, Object> map = new HashMap<>();

        List<Borrow> borrowList = borrowMapper.selectList(new QueryWrapper<Borrow>().eq("tool",toolType.getTool()));
        map.put("msg","success");
        map.put("records",borrowList);

        return map;
    }


    @RequestMapping("/tool/return")
    String toolReturn(@RequestBody Borrow toolType) {
        int num = borrowMapper.update(toolType, new UpdateWrapper<Borrow>().eq("id",toolType.getId()));
        if(num > 0 ){
            num = toolMapper.update(null,new UpdateWrapper<Tool>().setSql("num=  CAST(CAST(num AS UNSIGNED) + "+ toolType.getNum() +" AS CHAR)" ).eq("name",toolType.getTool()));
        }

        return num > 0 ? "success" : "error";
    }



    @RequestMapping("/stat/statType")
    Object statType() {

        Map<String, Object> map = new HashMap<>();

        List<Map<String, Object>> statList1 = toolMapper.statType();
        List<Map<String, Object>> statList2 = toolMapper.statSupply();
        List<Map<String, Object>> statList3 = toolMapper.statTypeTotal();
        List<Map<String, Object>> statList4 = toolMapper.statBorrow();
        List<Map<String, Object>> statList5 = toolMapper.statReturn();
        map.put("msg","success");

        map.put("records1",statList1);
        map.put("records2",statList2);
        map.put("records3",statList3);
        map.put("records4",statList4);
        map.put("records5",statList5);

        return map;
    }


    @RequestMapping("/supply/list")
    Object supplyList(@RequestBody Supply supply) {
        HashMap<String ,Object> map = new HashMap<>();
        List<Supply> list = supplyMapper.selectList(new QueryWrapper<>(supply));
        map.put("msg","success");
        map.put("data",list);
        return map;
    }


    @RequestMapping("/supply/add")
    String supplyAdd(@RequestBody Supply supply) {
        int num  = supplyMapper.insert(supply);
        return num > 0 ? "success" : "error";
    }

    @RequestMapping("/supply/edit")
    String supplyEdit(@RequestBody Supply supply) {
        int num  = supplyMapper.updateById(supply);
        return num > 0 ? "success" : "error";
    }

    @RequestMapping("/supply/del")
    String supplyDel(@RequestBody Supply supply) {
        int num  = supplyMapper.deleteById(supply);
        return num > 0 ? "success" : "error";
    }



























}
